package com.quinn.myDemo;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;


/**
 * Demo0412
 *
 * @author HanQuinn
 * Date: 2022/4/12
 * Time: 14:48
 */
public class Demo0412 {
    private static String getSignature(String appkey,String appSecret,String timestamp) {
        JSONObject info = new JSONObject();
        info.put("appkey", appkey);
        info.put("appSecret", appSecret);
        info.put("timestamp", timestamp);
        String parameterMap = info.toString();
        System.out.println(parameterMap);
        String mapSha256 = DigestUtil.sha256Hex(parameterMap);
        byte[] encode = Base64.getUrlEncoder().encode(Base64.getDecoder().decode(mapSha256));
        String s = encode.toString();
        return s;
    }

    private static void get() throws URISyntaxException, IOException {
        String appkey = "b7a1c3a02a744998b0465fa7f2c3bdf8";
        String appSecret = "a459df9c3648449da0e54ae85eac7c32";
        String timestamp = String.valueOf(System.currentTimeMillis());
        //创建httpClient对象
        CloseableHttpClient httpClient = createSSLClientDefault();
        URI uri = new URIBuilder("https://dbox.diwork.com/iuap-api-auth/open-auth/selfAppAuth/getAccessToken")
                .addParameter("appkey",appkey)
                .addParameter("timestamp",timestamp)
                .addParameter("signature",getSignature(appkey,appSecret,timestamp))
                .build();
        System.out.println(uri);
        //创建http get请求
        HttpGet httpGet = new HttpGet(uri);

        //接受请求返回的定义
        CloseableHttpResponse response = null;
        try {
            //执行get请求
            response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
    }

    public static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有
                @Override
                public boolean isTrusted(X509Certificate[] chain,
                                         String authType) throws CertificateException {
                    return true;
                }
            }).build();

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }

    public static void main(String[] args) throws Exception {
        System.out.println(System.currentTimeMillis());
    }
}
